/*
The MIT License
Copyright (c) 2019 Lehrstuhl Informatik 11 - RWTH Aachen University
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE

This file is part of embeddedRTPS.

Author: i11 - Embedded Software, RWTH Aachen University
*/

#ifndef RTPS_PARTICIPANT_H
#define RTPS_PARTICIPANT_H

#include "rtps/common/types.h"
#include "rtps/config.h"
#include "rtps/discovery/SEDPAgent.h"
#include "rtps/discovery/SPDPAgent.h"
#include "rtps/messages/MessageReceiver.h"

typedef struct rtps_Participant {
  GuidPrefix_t m_guidPrefix;
  ParticipantId_t m_participantId;
  struct MessageReceiver m_receiver;
  bool m_hasBuilInEndpoints;
  uint8_t m_nextUserEntityId[3];
  struct rtps_Writer *m_writers[NUM_WRITERS_PER_PARTICIPANT];
  uint8_t m_numWriters;
  struct rtps_Reader *m_readers[NUM_READERS_PER_PARTICIPANT];
  uint8_t m_numReaders;

  sys_mutex_t m_mutex;
  struct rtps_MemoryPool *m_remoteParticipants;
  struct rtps_SPDPAgent m_spdpAgent;
  struct rtps_SEDPAgent m_sedpAgent;
}rtps_Participant;

void rtps_Participant_Construct(struct rtps_Participant *Participant, 
                       const GuidPrefix_t *guidPrefix,
                       ParticipantId_t *participantId);
void rtps_Participant_Destruct(rtps_Participant *pParticipant);
bool rtps_Participant_isValid(rtps_Participant *pParticipant);
void rtps_Participant_reuse(rtps_Participant *pParticipant, const GuidPrefix_t *guidPrefix, ParticipantId_t participantId);
  // Actually the only two function that should be used by the user
bool rtps_Participant_registerOnNewPublisherMatchedCallback(rtps_Participant *pParticipant, void (*callback)(void *arg),
                                             void *args);
bool rtps_Participant_registerOnNewSubscriberMatchedCallback(rtps_Participant *pParticipant, void (*callback)(void *arg),
                                              void *args);
  //! Not-thread-safe function to add a writer
struct rtps_Writer *rtps_Participant_addWriter(rtps_Participant *pParticipant, struct rtps_Writer *writer);
bool rtps_Participant_isWritersFull(rtps_Participant *pParticipant);

uint8_t *rtps_Participant_getNextUserEntityKey(rtps_Participant *pParticipant);

  //! Not-thread-safe function to add a reader
struct rtps_Reader *rtps_Participant_addReader(rtps_Participant *pParticipant, struct rtps_Reader *reader);
bool rtps_Participant_isReadersFull(rtps_Participant *pParticipant);

  //! (Probably) Thread safe if writers cannot be removed
struct rtps_Writer *rtps_Participant_getWriter(rtps_Participant *pParticipant, EntityId_t id);
struct rtps_Writer *rtps_Participant_getMatchingWriter(rtps_Participant *pParticipant, const TopicData *topicData);
struct rtps_Writer *rtps_Participant_getMatchingWriter_compressed(rtps_Participant *pParticipant, const TopicDataCompressed *topicData);

  //! (Probably) Thread safe if readers cannot be removed
struct rtps_Reader *rtps_Participant_getReader(rtps_Participant *pParticipant, EntityId_t id);
struct rtps_Reader *rtps_Participant_getReaderByWriterId(rtps_Participant *pParticipant, const Guid_t *guid);
struct rtps_Reader *rtps_Participant_getMatchingReader(rtps_Participant *pParticipant, const TopicData *topicData);
struct rtps_Reader *rtps_Participant_getMatchingReader_compressed(rtps_Participant *pParticipant,const TopicDataCompressed *topicData);

bool rtps_Participant_addNewRemoteParticipant(rtps_Participant *pParticipant, const ParticipantProxyData *remotePart);
bool rtps_Participant_removeRemoteParticipant(rtps_Participant *pParticipant, const GuidPrefix_t *prefix);
void rtps_Participant_removeAllEntitiesOfParticipant(rtps_Participant *pParticipant, const GuidPrefix_t *prefix);
ParticipantProxyData *rtps_Participant_findRemoteParticipant(rtps_Participant *pParticipant, const GuidPrefix_t *prefix);
void rtps_Participant_findRemoteParticipant_free(ParticipantProxyData *proxyData);
void rtps_Participant_refreshRemoteParticipantLiveliness(rtps_Participant *pParticipant, const GuidPrefix_t *prefix);
uint32_t rtps_Participant_getRemoteParticipantCount(rtps_Participant *pParticipant);
MessageReceiver *rtps_Participant_getMessageReceiver(rtps_Participant *pParticipant);
void rtps_Participant_addHeartbeat(rtps_Participant *pParticipant, GuidPrefix_t sourceGuidPrefix);
bool rtps_Participant_checkAndResetHeartbeats(rtps_Participant *pParticipant);

bool rtps_Participant_hasReaderWithMulticastLocator(rtps_Participant *pParticipant, ip4_addr_t address);

void rtps_Participant_addBuiltInEndpoints(rtps_Participant *pParticipant, BuiltInEndpoints *endpoints);
void rtps_Participant_newMessage(rtps_Participant *pParticipant, const uint8_t *data, DataSize_t size);

struct rtps_SPDPAgent *rtps_Participant_getSPDPAgent(rtps_Participant *pParticipant);
#endif // RTPS_PARTICIPANT_H
